package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Connection con = f.newConnection();//连接
        Channel c = con.createChannel();//通信通道

        //创建交换机
        //服务器端创建交换记，如果交换记已经存在不会重新创建
        /*
        s:代表名字，叫什么都行
        s1: 表示类型
         */
//        c.exchangeDeclare("logs", "fanout");
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);

        //向交换记发送消息，而不是向队列发送
        while (true){
            System.out.println("输入消息：");
            String s = new Scanner(System.in).nextLine();
            //第二个参数不指定队列，指定也无效
            c.basicPublish("logs", "",null,s.getBytes(StandardCharsets.UTF_8));
        }
    }
}
